package com.kstudio.ksnapshot.kedit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.kstudio.ksnapshot.KUtil;
import com.kstudio.ksnapshot.view.KSeekView;
import com.kstudio.ksnapshot.view.KSeekView.KSeekViewListener;

public class KEditControl extends ViewGroup implements OnClickListener, KSeekViewListener {
    private KUtil kLog = new KUtil("KEditControl");

    private Context mContext;

    private LayoutInflater inflate;

    private KEditControlLayout mLayout = new KEditControlLayout();
    private Button mBtnSingleSave;
    private Button mBtnContinuousSave;
    private KSeekView kSeekView;

    private int mWidth;
    private int mHeight;

    private KEditControlListener mKEditControlListener;
    private int kDuration;
    
    
    private class KEditControlLayout {
        int LAYOUT_TOP_PADDING = 0;
        int LAYOUT_LEFT_PADDING = 0;
        int LAYOUT_RIGHT_PADDING = 0;
        int BTN_GAP_PADDING = 0;

        int BTN_SINGLESAVE_WIDTH = 0;
        int BTN_SINGLESAVE_HEIGHT = 0;

        int BTN_CONTINUOUSSAVE_WIDTH = 0;
        int BTN_CONTINUOUSSAVE_HEIGHT = 0;
    }

    public interface KEditControlListener {
        void onSingleSaveClick();
        void onSeekViewChangeTime(int t);
    }

    public KEditControl(Context context) {
        this(context, null);
    }

    public KEditControl(Context context, AttributeSet attrs) {
        this(context, attrs , 0);
    }

    public KEditControl(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mContext = context;
        mKEditControlListener = null;

        inflate = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mWidth = 0;
        mHeight = 0;

        kDuration = 0;
        
        KcreateLayout();
        KsetAttribute(attrs,defStyle);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if(changed) {
            kLog.i("onLayout changed l = %d t = %d r = %d b = %d",l,t,r,b);
            mWidth = r - l;
            mHeight = b - t;
            KchangeLayout(l, t, r, b);
        } else {

        }

    }

    @Override
    public void onClick(View v) {
        if(mBtnSingleSave == v ) {
            KSingleSaveClick();
        }
        kLog.i("KSingleSave : onClick");
    }

    public void KSetOnEditControlListener(KEditControlListener l) {
        mKEditControlListener = l;
    }

    public void KSetDuration(int duration) {
        kSeekView.KSetDuration(duration);
    }
    
    private void KsetAttribute(AttributeSet attrs, int defStyle) {
        int[] ids = new int[attrs.getAttributeCount()];
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            ids[i] = attrs.getAttributeNameResource(i);
        }

        TypedArray a = mContext.obtainStyledAttributes(attrs, ids, defStyle, 0);

        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            if (attrName == null)
                continue;

            if (attrName.equals("layout_top_padding")) {
                mLayout.LAYOUT_TOP_PADDING = a.getDimensionPixelSize(i, 0);
                kLog.i("LAYOUT_TOP_PADDING %d", mLayout.LAYOUT_TOP_PADDING);
            } else if (attrName.equals("layout_left_padding")) {
                mLayout.LAYOUT_LEFT_PADDING = a.getDimensionPixelSize(i, 0);
            } else if (attrName.equals("layout_right_padding")) {
                mLayout.LAYOUT_RIGHT_PADDING = a.getDimensionPixelSize(i, 0);
            } else if (attrName.equals("btn_gap_padding")) {
                mLayout.BTN_GAP_PADDING = a.getDimensionPixelSize(i, 0);
            } else if (attrName.equals("singlesave_drawable")) {
                Drawable d = a.getDrawable(i);
                Drawable e = d.getCurrent();
                mLayout.BTN_SINGLESAVE_WIDTH = ((BitmapDrawable)(e)).getBitmap().getWidth();
                mLayout.BTN_SINGLESAVE_HEIGHT = ((BitmapDrawable)(e)).getBitmap().getHeight();

                mBtnSingleSave.setBackground(d);
            } else if (attrName.equals("continuoussave_drawable")) {
                Drawable d = a.getDrawable(i);
                Drawable e = d.getCurrent();
                mLayout.BTN_CONTINUOUSSAVE_WIDTH = ((BitmapDrawable)(e)).getBitmap().getWidth();
                mLayout.BTN_CONTINUOUSSAVE_HEIGHT = ((BitmapDrawable)(e)).getBitmap().getHeight();

                mBtnContinuousSave.setBackground(d);
            } else if (attrName.equals("SeekBarEdgePadding")) {
                kSeekView.KSetSeekBarPaddingEdge(a.getDimensionPixelSize(i,0));
            } else if (attrName.equals("SeekBarTopPadding")) {
                kSeekView.KSetSeekBarPaddingTop(a.getDimensionPixelSize(i,0));
            } else if (attrName.equals("SeekBarDownPadding")) {
                kSeekView.KSetSeekBarPaddingDown(a.getDimensionPixelSize(i,0));
            } else if (attrName.equals("SeekBarEdge")) {
                int id = a.getResourceId(i, 0);
                kSeekView.KSetSeekBarEdge(id);
            } else if (attrName.equals("SeekBarMiddle")) {
                int id = a.getResourceId(i, 0);
                kSeekView.KSetSeekBarMiddle(id);
            } else if (attrName.equals("SeekControlNormal")) {
                int id = a.getResourceId(i, 0);
                kSeekView.KSetSeekControlNormal(id);
            } else if (attrName.equals("SeekTextSize")) {
                int size = a.getDimensionPixelSize(i, 0);
                kSeekView.KSetTextSize(size);
            }
        }

        a.recycle();
    }

    private void KcreateLayout() {
        mBtnSingleSave = new Button(mContext);
        mBtnSingleSave.setOnClickListener(this);
        addView(mBtnSingleSave);

        mBtnContinuousSave = new Button(mContext);
        addView(mBtnContinuousSave);

        kSeekView = new KSeekView(mContext);
        kSeekView.KSetSeekViewListener(this);
        addView(kSeekView);
    }

    private void KchangeLayout(int l, int t, int r, int b) {
        int left = l;
        int top = t;
        int right = 0;
        int bottom = 0;

        if(mBtnSingleSave != null) {
            left = mLayout.LAYOUT_LEFT_PADDING;
            top = mLayout.LAYOUT_TOP_PADDING;
            right = left + mLayout.BTN_SINGLESAVE_WIDTH;
            bottom = top + mLayout.BTN_SINGLESAVE_HEIGHT;
            mBtnSingleSave.layout(left, top, right, bottom);
        }

        if(mBtnContinuousSave != null) {
            left = mLayout.LAYOUT_LEFT_PADDING;
            if(mBtnSingleSave != null) {
                top = bottom + mLayout.BTN_GAP_PADDING;
            } else {
                top = mLayout.LAYOUT_TOP_PADDING;
            }
            right = left + mLayout.BTN_CONTINUOUSSAVE_WIDTH;
            bottom = top + mLayout.BTN_CONTINUOUSSAVE_HEIGHT;
            mBtnContinuousSave.layout(left, top, right, bottom);
        }

        if(kSeekView != null) {
            left = l + mLayout.LAYOUT_LEFT_PADDING;
            top = b / 2;
            right = r - mLayout.LAYOUT_RIGHT_PADDING;
            bottom = b - 50;
//            kLog.e("left = %d top = %d right = %d bottom = %d", left, top,right,bottom);
            kSeekView.layout(left, top, right, bottom);
        }
    }

    private void KSingleSaveClick() {
        if(mKEditControlListener != null) {
            mKEditControlListener.onSingleSaveClick();
        } else {
            kLog.i("KSingleSaveClick : mKEditControlListener is null");
        }
    }

    @Override
    public void onKSeekViewChangeTime(int time) {
        if(mKEditControlListener != null) mKEditControlListener.onSeekViewChangeTime(time);
    }
}
